package com.ruoyi.web.controller.api.recipe;

import com.ruoyi.api.domain.recipe.DishDetailEntity;
import com.ruoyi.api.domain.recipe.DishMealEntity;
import com.ruoyi.api.domain.recipe.DishMealItemEntity;
import com.ruoyi.common.annotation.NoLoginRequired;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.recipe.domain.*;
import com.ruoyi.recipe.service.IRecipeArticleService;
import com.ruoyi.web.controller.api.APIController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api("文章")
@RestController
@RequestMapping("/api/article")
@NoLoginRequired
public class ArticleController extends APIController
{
    @Autowired
    private IRecipeArticleService recipeArticleService;

    @ApiOperation("获取文章列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keywords", value = "搜索值", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "pageNum", value = "当前记录起始索引", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", dataType = "Integer", dataTypeClass = Integer.class)
    })
    @PostMapping("/list")
    public TableDataInfo list(@RequestParam(value = "keywords", required = false, defaultValue = "") String keywords) {
        String baseUrl = getBaseUrl();

        RecipeArticle parameterEntity = new RecipeArticle();
        if (!StringUtils.isEmpty(keywords)) {
            parameterEntity.setSearchValue(keywords.trim());
        }
        parameterEntity.setStatus("0");

        startPage();

        startOrderBy("createTime", "desc");

        List<RecipeArticle> list = recipeArticleService.selectSimpleRecipeArticleList(parameterEntity);
        for (RecipeArticle article : list) {
            if (StringUtils.isNotEmpty(article.getArticlePic()) && !StringUtils.ishttp(article.getArticlePic())) {
                String fullArticlePic = baseUrl + article.getArticlePic();
                article.setArticlePic(fullArticlePic);
            }
        }

        return getDataTable(list);
    }

    @ApiOperation("修改浏览次数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleId", value = "文章ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "num", value = "增加或者减少的浏览次数", dataType = "Integer", dataTypeClass = Integer.class)
    })
    @PostMapping("/updateViews")
    public AjaxResult updateViewsCount(@RequestParam(value = "articleId", required = true) Long articleId,
                                       @RequestParam(value = "num", required = true) int num) {
        return toAjax(recipeArticleService.updateViewsCount(new Long[]{articleId}, num));
    }

    @ApiOperation("修改点赞次数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleId", value = "文章ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "num", value = "增加或者减少的点赞次数", dataType = "Integer", dataTypeClass = Integer.class)
    })
    @PostMapping("/updateLikes")
    public AjaxResult updateLikesCount(@RequestParam(value = "articleId", required = true) Long articleId,
                                       @RequestParam(value = "num", required = true) int num) {
        return toAjax(recipeArticleService.updateLikesCount(new Long[]{articleId}, num));
    }

    @ApiOperation("获取文章详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleId", value = "文章ID", required = true, dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "openId", value = "微信openId", dataType = "String", dataTypeClass = String.class)
    })
    @GetMapping("/detail")
    public AjaxResult getArticleDetail(Long articleId, String openId) {
        RecipeArticle recipeArticle = recipeArticleService.selectRecipeArticleByArticleId(articleId);
        if (recipeArticle == null) return error("文章不存在");

        recipeArticleService.updateViewsCount(new Long[]{articleId}, 1);

        return AjaxResult.success("操作成功", recipeArticle);
    }
}
